package crons

import (
	"context"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/frame/g"
	"hotgo/internal/consts"
	"hotgo/internal/dao"
	"hotgo/internal/library/cron"
	"hotgo/internal/library/hgorm"
	"hotgo/internal/model/entity"
	"hotgo/internal/model/input/sysin"
	"hotgo/internal/service"
	"time"
)

func init() {
	cron.Register(AuctionEnd)
}

var AuctionEnd = &cAuctionEnd{name: "auction_end"}

type cAuctionEnd struct {
	name string
}

func (c *cAuctionEnd) GetName() string {
	return c.name
}

// Execute 执行任务
func (c *cAuctionEnd) Execute(ctx context.Context, parser *cron.Parser) (err error) {
	// 业务逻辑
	var (
		mapperList []entity.AuctionProductMapper
	)
	g.DB().Transaction(ctx, func(ctx context.Context, tx gdb.TX) error {
		// 查询可用竞拍活动
		if err = service.SysAuctionProductMapper().Model(ctx).
			FieldsPrefix(dao.AuctionProductMapper.Table(), sysin.AuctionProductMapperListModel{}).
			Fields(hgorm.JoinFields(ctx, sysin.AuctionProductMapperListModel{}, &dao.Auction, "auction")).
			LeftJoinOnFields(dao.Auction.Table(), dao.AuctionProductMapper.Columns().AuctionId, "=", dao.Auction.Columns().Id).
			Where(dao.AuctionProductMapper.Columns().Status, consts.AuctionStatusWorking).
			Wheref("end_time <= ?", time.Now().Format(time.TimeOnly)).
			WhereOrf("hg_auction.end_date < ?", time.Now().Format(time.DateOnly)).
			//WhereLTE(dao.AuctionProductMapper.Columns().EndTime, time.Now().Format(time.TimeOnly)).
			Scan(&mapperList); err != nil {
			parser.Logger.Warning(ctx, "cron AuctionEnd Execute err: ", err)
			return err
		}
		for _, mapper := range mapperList {
			if _, err = service.SysAuctionProductMapper().Model(ctx).
				WherePri(mapper.Id).
				Data(g.Map{
					dao.AuctionProductMapper.Columns().Status: consts.AuctionStatusEnd,
				}).Update(); err != nil {
				parser.Logger.Warning(ctx, "cron AuctionEnd Execute err: ", err)
				return err
			}

			var (
				logList    []entity.AuctionLog
				endLogList []entity.AuctionLog
			)
			if err = service.SysAuctionLog().Model(ctx).
				Where(dao.AuctionLog.Columns().MapperId, mapper.Id).
				Scan(&logList); err != nil {
				parser.Logger.Warning(ctx, "cron AuctionEnd Execute err: ", err)
			}
			if err = service.SysAuctionLog().Model(ctx).
				Where(dao.AuctionLog.Columns().MapperId, mapper.Id).
				Where(dao.AuctionLog.Columns().Status, consts.AuctionLogEnd).
				Scan(&endLogList); err != nil {
				parser.Logger.Warning(ctx, "cron AuctionEnd Execute err: ", err)
			}
			if len(logList) == len(endLogList) {
				if _, err = service.SysAuctionProductMapper().Model(ctx).
					WherePri(mapper.Id).
					Data(g.Map{
						dao.AuctionProductMapper.Columns().Status: consts.AuctionStatusEnd,
					}).Update(); err != nil {
					parser.Logger.Warning(ctx, "cron AuctionEnd Execute err: ", err)
				}
			}

			//if err = createOrder(ctx, mapper); err != nil {
			//	parser.Logger.Warning(ctx, "cron AuctionEnd Execute err: ", err)
			//	return err
			//}
		}
		return err
	})

	return
}

//func createOrder(ctx context.Context, mapper entity.AuctionProductMapper) error {
//	var (
//		logList []entity.AuctionLog
//		product entity.Product
//	)
//	if err := service.SysAuctionLog().Model(ctx).WhereIn(dao.AuctionLog.Columns().MapperId, mapper.Id).Scan(&logList); err != nil {
//		return err
//	}
//	if err := service.SysProduct().Model(ctx).WherePri(mapper.ProductId).Scan(&product); err != nil {
//		return err
//	}
//	for _, log := range logList {
//		_, err := service.SysUserOrder().OrderPay(ctx, &sysin.OrderPayInp{
//			ProductID:    product.Id,
//			Qty:          uint(mapper.Count),
//			AuctionLogId: log.Id,
//		})
//		if err != nil {
//			return err
//		}
//	}
//	return nil
//}
